package ro.msg.quiz.repository;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

/**
 * @author: Vlad
 * 
 *          This class extends Question class. Text question is a particular
 *          type of question. It has a correct answer of text type.
 */
@XmlRootElement(name = "TextQ")
@XmlAccessorType(XmlAccessType.PROPERTY)
public class TextQ extends Question {
	// answer is a string with
	private String answer;
	private static final int type = ClassType.TEXT_QUESTION.getCode();
	private static String dateFormatString = "dd.MM.yyyy";

	/**
	 * Constructor
	 */
	public TextQ() {
		this.setId(-1);
		this.setType(3);
		this.setText(null);
		this.setAnswer(null);

		DateFormat dateFormat = new SimpleDateFormat(dateFormatString);
		Date date = new Date();
		setLastEdited(dateFormat.format(date));
	}

	/**
	 * Constructor
	 * 
	 * @param id
	 *            The id of question.
	 * @param text
	 *            The text of question.
	 * @param answer
	 *            The answer of question.
	 */
	public TextQ(int id, String text, String answer) {
		this.setId(id);
		this.setType(3);
		this.setText(text);
		this.setAnswer(answer);

		DateFormat dateFormat = new SimpleDateFormat(dateFormatString);
		Date date = new Date();
		setLastEdited(dateFormat.format(date));
	}

	/**
	 * 
	 * @return Returns the answer of question.
	 */
	@XmlElement
	public String getAnswer() {
		return answer;
	}

	/**
	 * @return Returns the correct answer
	 */
	@Override
	public ArrayList<String> getAnswers() {
		ArrayList<String> answer = new ArrayList<String>();
		answer.add(this.answer);
		return answer;
	}

	/**
	 * Sets the answer of question.
	 * 
	 * @param answer
	 *            The answer of question to be set.
	 */
	public void setAnswer(String answer) {
		this.answer = answer;

		DateFormat dateFormat = new SimpleDateFormat(dateFormatString);
		Date date = new Date();
		setLastEdited(dateFormat.format(date));
	}

	/**
	 * Returns and arrayList of the question options
	 * 
	 * @see ro.msg.quiz.repository.Question#getOptions() return
	 *      ArrayList<String>
	 */
	@Override
	public ArrayList<String> getOptions() {
		ArrayList<String> options = new ArrayList<String>();
		options.add(this.answer);
		return options;
	}

	/**
	 * @return Returns the type of question.
	 */
	@Override
	public int getType() {
		return type;
	}

	/**
	 * This method prints the id, text, answer of text question.
	 */
	@Override
	public String toString() {
		return getId() + " " + getText() + " " + answer;
	}
}
